package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.GoodsClassificationAdapter;
import com.zmx.doctor.fragment.TwoFragment;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.GoodsClassification;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsDetailsActivity extends BaseActivity {

    @BindView(R.id.goods_image)
    ImageView goodsImage;
    @BindView(R.id.goods_name)
    TextView goodsName;
    @BindView(R.id.goods_price)
    TextView goodsPrice;
    @BindView(R.id.goods_describe)
    TextView goodsDescribe;
    @BindView(R.id.add_cart)
    Button addCart;

    private String goods_id;
    private Integer nums = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_details;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        goods_id = this.getIntent().getStringExtra("goods_id");
        getGoods();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);


            switch (msg.what) {

                case 1:


                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());

                        JSONObject data = object.getJSONObject("data");
                        Glide.with(mActivity).load(data.getString("img")).into(goodsImage);
                        goodsName.setText(data.getString("name"));
                        goodsPrice.setText("￥" + data.getString("price"));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            goodsDescribe.setText(Html.fromHtml((data.getString("describe")), 10));
                        }

                    } catch (JSONException e) {

                        e.printStackTrace();
                    }

                    break;

                case 2:


                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast(object.getString("msg"));

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    public void getGoods() {

        List<String> l = new ArrayList<>();
        l.add(goods_id);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getGoods");
        params.put("data[goods_id]", goods_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 1, 404);

    }

    public void editCart() {

        nums = nums+1;
        List<String> l = new ArrayList<>();
        l.add(goods_id);
        l.add(nums+"");

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "editCart");
        params.put("data[goods_id]", goods_id);
        params.put("data[nums]", nums+"");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 2, 404);

    }

    @OnClick(R.id.add_cart)
    public void onViewClicked() {

        editCart();

    }


}
